/* This program is free software. It comes without any warranty, to
 * the extent permitted by applicable law. You can redistribute it
 * and/or modify it under the terms of the Do What The Fuck You Want
 * To Public License, Version 2, as published by Sam Hocevar. See
 * http://sam.zoy.org/wtfpl/COPYING for more details. */ 
package de.rockon.fuzzy.controller.gui.propertyeditor;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import de.rockon.fuzzy.controller.model.FuzzyBasicElement;
import de.rockon.fuzzy.controller.model.FuzzySet;
import de.rockon.fuzzy.controller.model.enums.ColorType;
import de.rockon.fuzzy.controller.util.Logger;
import de.rockon.fuzzy.controller.util.factories.UIFactory;
import de.rockon.fuzzy.exceptions.NoValueEnteredException;

public class PropertySet extends PropertyBase {
	private static final long serialVersionUID = 7442074495227832679L;
	private JLabel lblColor;
	private JComboBox cmbColor;
	private JLabel lblName;
	private JTextField txtName;
	private FuzzySet property;
	private ColorType colorType;

	public PropertySet() {
		initGUI();
	}

	@Override
	public void getPropertyValues(FuzzyBasicElement<?, ?> property) {
		this.property = (FuzzySet) property;

		resetProperties();
	}

	@Override
	public void initGUI() {
		FormLayout layout = new FormLayout("1px, right:pref, 1dlu, pref:grow, 1px", "1px, pref, 1dlu, pref, 1dlu, pref, 1px");

		PanelBuilder builder = new PanelBuilder(layout);
		CellConstraints cc = new CellConstraints();

		lblName = UIFactory.getJLabel("Name: ", null, SwingConstants.RIGHT);
		txtName = UIFactory.getJTextField("new variable", 10, null, this);

		lblColor = UIFactory.getJLabel("Color: ", null, SwingConstants.RIGHT);

		cmbColor = new JComboBox();
		cmbColor.setRenderer(new ColorBoxRenderer());
		for (ColorType color : ColorType.values()) {
			cmbColor.addItem(color);
		}
		cmbColor.setEditable(false);
		cmbColor.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				colorType = (ColorType) cmbColor.getSelectedItem();
				property.setColorType(colorType);
			}
		});

		// setze alle Tooltips
		String toolTipName = "The name of the new variable";
		String toolTipColor = "sets the Color of the current Set";

		lblName.setToolTipText(toolTipName);
		txtName.setToolTipText(toolTipName);
		lblColor.setToolTipText(toolTipColor);
		cmbColor.setToolTipText(toolTipColor);

		builder.addSeparator("Set Properties", cc.xyw(1, 2, 5));
		builder.add(lblName, cc.xy(2, 4));
		builder.add(txtName, cc.xy(4, 4));

		builder.add(lblColor, cc.xy(2, 6));
		builder.add(cmbColor, cc.xy(4, 6));

		add(builder.getPanel());
	}

	@Override
	public void resetProperties() {
		if (property != null) {
			txtName.setText("" + property.getName());
			cmbColor.setSelectedItem(property.getColorType());
		}
	}

	@Override
	public void saveProperties() {
		try {
			Logger.info("--------------------------1 new Color: " + txtName.getText());
			property.setName(txtName.getText());
			Logger.info("--------------------------1 new Color: " + colorType);
			property.setColorType(colorType);

		} catch (NoValueEnteredException e) {
			UIFactory.showErrorDialog(null, e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
			UIFactory.showErrorDialog(null, "The Inputs couldn't processed");
			e.printStackTrace();
		}
	}

}
